#!/bin/bash

# MySQL备份脚本-

# 配置信息
USER="root"
PASSWORD="Techbloom$2012"
HOST="192.168.4.50"


BACK_NAME="wms"
BACKUP_DIR="/mnt/d/BACKUP_FILES/DB/mysql_${BACK_NAME}"
TMP_DIR="/mnt/d/BACKUP_FILES/DB/mysql_${BACK_NAME}_backup_$DATE"  # 临时目录
DATE=$(date +%Y%m%d_%H%M%S)
ARCHIVE_FILE="$BACKUP_DIR/${BACK_NAME}_mysql_backup_${DATE}.tar.gz" #打包目录

# 确保备份目录存在
mkdir -p "$BACKUP_DIR"
mkdir -p "$TMP_DIR"


#备份erm数据库
mysqldump  -u "$USER" -p'Techbloom$2012' -h "$HOST" bjwms  | gzip  > $TMP_DIR/wms_$DATE.sql.gz 



echo "正在打包所有备份文件到: $ARCHIVE_FILE"

if [ -n "$(ls -A $TMP_DIR 2>/dev/null)" ]; then
    tar -vczf "$ARCHIVE_FILE" -C "$TMP_DIR" .
    if [ $? -eq 0 ]; then
        echo "备份文件打包成功"
    else
        echo "错误: 备份文件打包失败"
    fi
else
    echo "没有找到需要打包的备份文件"
fi

# 删除临时目录
rm -rf "$TMP_DIR"

find $BACKUP_DIR -mtime +20 -exec rm -Rf {} \;

echo "所有数据库备份完成!"    